package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.EquipmentRepairRemark;
import com.mxpio.erp.common.equipment.service.EquipmentRepairRemarkService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Tag(name = "EquipmentRepairRemarkController", description = "设备维修-重要备注")
@RestController
@RequestMapping("/erp/equipment/spotrepair/remark")
public class EquipmentRepairRemarkController {

    @Autowired
    private EquipmentRepairRemarkService remarkService;

    @GetMapping("list")
    @Operation(summary = "重要备注列表", description = "重要备注列表（无分页）", method = "GET")
    public Result<List<EquipmentRepairRemark>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentRepairRemark> list = remarkService.list(EquipmentRepairRemark.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "重要备注列表", description = "重要备注列表（分页）", method = "GET")
    public Result<Page<EquipmentRepairRemark>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EquipmentRepairRemark> pageResult = remarkService.listPage(EquipmentRepairRemark.class, page, criteria);
        return Result.OK(pageResult);
    }



    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据工单编码获取重要备注列表", description = "根据工单编码获取重要备注列表", method = "GET")
    public Result<EquipmentRepairRemark> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
        EquipmentRepairRemark remark = remarkService.getById(EquipmentRepairRemark.class, bizNo);
        return Result.OK(remark);
    }

    @RequestMapping("add")
    @Operation(summary = "新增重要备注", description = "新增重要备注")
    public Result<EquipmentRepairRemark> add(@RequestBody EquipmentRepairRemark remark) {
        remarkService.save(remark);
        return Result.OK(remark);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑重要备注", description = "编辑重要备注", method = "PUT")
    public Result<EquipmentRepairRemark> edit(@RequestBody EquipmentRepairRemark remark) {
        remarkService.update(remark);
        return Result.OK(remark);
    }
    @DeleteMapping(value = "delete/{ids}")
    @Operation(summary = "批量删除", description = "批量删除", method = "DELETE")
    public Result<?> delete(@PathVariable(name = "ids", required = true) String ids) {
        Criteria criteria = Criteria.create();
        criteria.addCriterion("id", Operator.IN, Arrays.asList(ids.split(",")));
        this.remarkService.removeBatch(EquipmentRepairRemark.class,criteria);
        return Result.OK("批量删除成功!");
    }
}
